<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>票据粘贴单模版</title>
    <style>
        body {
            font-family: 'SimSun', '宋体', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #fff;
            display: flex;
            justify-content: center;
        }
        .container {
            background-color: white;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            position: relative;
            margin: 20px;
            max-width: 1200px;
            border-radius: 8px;
        }
        .paste-sheet {
            border: 2px solid #333;
            position: relative;
            margin: 20px;
            padding: 20px;
            background-color: white;
            transition: all 0.3s ease;
        }
        /* 背景颜色选项 */
        .paste-sheet.bg-white {
            background-color: white;
        }
        .paste-sheet.bg-blue {
            background-color: #e6f4ff;
        }
        .paste-sheet.bg-green {
            background-color: #e8f5e9;
        }
        .paste-sheet.bg-yellow {
            background-color: #fff9e6;
        }
        .paste-sheet.bg-gray {
            background-color: #f5f5f5;
        }
        .sheet-header {
            text-align: center;
            margin-bottom: 20px;
        }
        .sheet-title {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        .sheet-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            font-size: 16px;
        }
        .sheet-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .sheet-table th, .sheet-table td {
            border: 1px solid #333;
            padding: 8px;
            text-align: center;
        }
        .sheet-table th {
            font-weight: bold;
            background-color: #f0f0f0;
        }
        .amount-cell {
            text-align: right;
        }
        .sheet-footer {
            margin-top: 20px;
            font-size: 16px;
        }
        .controls-container {
            position: fixed;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            z-index: 100;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .print-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .print-btn:hover {
            background: linear-gradient(135deg, #5a6fc5 0%, #6a3e91 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }
        .print-btn:active {
            transform: translateY(0);
        }
        .editable {
            border-bottom: 1px dotted #666;
            width: 120px;
            display: inline-block;
            padding: 2px 5px;
            text-align: left;
            transition: all 0.2s ease;
        }
        .editable:focus {
            outline: none;
            border-bottom: 2px solid #2196F3;
            background-color: #f9f9f9;
        }
        .amount-display {
            min-width: 150px;
            display: inline-block;
            text-align: right;
        }
        .signature-fields {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
            font-size: 16px;
        }
        .signature-fields > div {
            flex: 1;
            margin: 0 10px;
            text-align: center;
        }
        .date-input {
            display: inline-block;
            text-align: center;
            border: none;
            border-bottom: 1px dotted #333;
            margin: 0 5px;
            transition: all 0.2s ease;
            background: transparent;
        }
        .date-input:focus {
            outline: none;
            border-bottom: 2px solid #2196F3;
            background-color: #f9f9f9;
        }
        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 60px;
            color: rgba(200, 200, 200, 0.2);
            z-index: 0;
            pointer-events: none;
            transform: translate(-50%, -50%) rotate(-15deg);
        }
        /* 背景选择器样式 */
        .bg-selector {
            background: white;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        .bg-selector h3 {
            margin-top: 0;
            margin-bottom: 10px;
            font-size: 16px;
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
        }
        .bg-option {
            display: block;
            margin: 5px 0;
            padding: 8px 10px;
            border-radius: 3px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
        }
        .bg-option:hover {
            background: #f0f0f0;
        }
        .bg-option.active {
            background: #e0e0ff;
            font-weight: bold;
            color: #333;
        }
        .bg-option .color-preview {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            margin-right: 8px;
            display: inline-block;
            border: 1px solid #ddd;
        }
        /* 打印样式 */
        @media print {
            body {
                background-color: white;
                padding: 0;
            }
            .container {
                max-width: none;
                box-shadow: none;
                padding: 0;
            }
            .paste-sheet {
                margin-top: 0;
                border: 2px solid #000;
                padding: 20px;
            }
            .controls-container, 
            .watermark,
            .add-row-btn,
            .delete-row-btn,
            .sheet-table th:nth-child(6), 
            .sheet-table td:nth-child(6) {
                display: none !important;
            }
            .editable {
                border-bottom: 1px dotted #000;
            }
            .date-input {
                border-bottom: 1px dotted #000;
            }
        }
        .paste-area {
            min-height: 300px;
            border: 1px dashed #999;
            margin: 20px 0;
            padding: 10px;
            background-color: rgba(240, 240, 240, 0.5);
        }
        .total-row {
            font-weight: bold;
            background-color: #f0f0f0;
        }
        .add-row-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 5px 10px;
            margin: 5px 0;
            cursor: pointer;
            border-radius: 3px;
        }

        .add-row-btn:hover {
            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }

        .delete-row-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 2px 5px;
            margin-left: 5px;
            cursor: pointer;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="pasteSheet" class="paste-sheet bg-white">
            <div class="watermark">票据粘贴单</div>
            <div class="sheet-header">
                <div class="sheet-title">票据粘贴单</div>
            </div>
            
            <div class="sheet-info">
                <div>单位名称: <span id="companyName" class="editable" contenteditable="true" style="width:200px;"></span></div>
                <div>日期: 
                    <input type="text" id="yearInput" class="date-input" placeholder="年" maxlength="4" style="width:40px;">年
                    <input type="text" id="monthInput" class="date-input" placeholder="月" maxlength="2" style="width:20px;">月
                    <input type="text" id="dayInput" class="date-input" placeholder="日" maxlength="2" style="width:20px;">日
                </div>
            </div>
            
            <div class="sheet-info">
                <div>部门: <span id="department" class="editable" contenteditable="true" style="width:120px;"></span></div>
                <div>经办人: <span id="operator" class="editable" contenteditable="true" style="width:120px;"></span></div>
                <div>单据张数: <span id="receiptCount" class="editable" contenteditable="true" style="width:50px;"></span></div>
            </div>
            
            <table class="sheet-table">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>票据内容</th>
                        <th>票据金额</th>
                        <th>票据张数</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="itemsTable">
                    <tr>
                        <td>1</td>
                        <td><span class="editable item-content" contenteditable="true"></span></td>
                        <td class="amount-cell"><span class="editable amount-input" contenteditable="true">0.00</span></td>
                        <td><span class="editable item-count" contenteditable="true">1</span></td>
                        <td><span class="editable item-note" contenteditable="true"></span></td>
                        <td><button class="delete-row-btn">删除</button></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr class="total-row">
                        <td colspan="2">合计</td>
                        <td class="amount-cell"><span id="totalAmount">0.00</span></td>
                        <td><span id="totalCount">1</span></td>
                        <td colspan="2"></td>
                    </tr>
                </tfoot>
            </table>
            
            <button id="addRowBtn" class="add-row-btn">添加一行</button>
            
            <div class="paste-area" id="pasteArea">
                <p style="text-align: center; color: #666; margin-top: 120px;">请在此处粘贴票据</p>
            </div>
            
            <div class="sheet-footer">
                <div>审核人: <span class="editable" contenteditable="true" style="width:80px;"></span></div>
                <div style="margin-top: 10px;">财务复核: <span class="editable" contenteditable="true" style="width:80px;"></span></div>
            </div>
        </div>
        
        <!-- 控制面板 -->
        <div class="controls-container">
            <!-- 背景选择器 -->
            <div class="bg-selector">
                <h3>选择背景</h3>
                <div class="bg-option active" data-bg="bg-white">
                    <span class="color-preview" style="background-color: white;"></span>
                    纯白色
                </div>
                <div class="bg-option" data-bg="bg-blue">
                    <span class="color-preview" style="background-color: #e6f4ff;"></span>
                    淡蓝色
                </div>
                <div class="bg-option" data-bg="bg-green">
                    <span class="color-preview" style="background-color: #e8f5e9;"></span>
                    淡绿色
                </div>
                <div class="bg-option" data-bg="bg-yellow">
                    <span class="color-preview" style="background-color: #fff9e6;"></span>
                    淡黄色
                </div>
                <div class="bg-option" data-bg="bg-gray">
                    <span class="color-preview" style="background-color: #f5f5f5;"></span>
                    淡灰色
                </div>
            </div>
            
            <!-- 打印按钮 -->
            <button type="button" id="printSheet" class="print-btn">
                <i class="fa fa-print mr-2"></i>打印粘贴单
            </button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 设置默认日期
            const now = new Date();
            document.getElementById('yearInput').value = now.getFullYear();
            document.getElementById('monthInput').value = now.getMonth() + 1;
            document.getElementById('dayInput').value = now.getDate();
            
            // 打印粘贴单
            document.getElementById('printSheet').addEventListener('click', function() {
                window.print();
            });
            
            // 监听所有金额输入变化
            document.querySelectorAll('.amount-input').forEach(input => {
                input.addEventListener('input', updateTotals);
            });
            
            // 监听所有票据张数变化
            document.querySelectorAll('.item-count').forEach(input => {
                input.addEventListener('input', updateTotals);
            });
            
            // 更新总计金额和票据张数
            function updateTotals() {
                let totalAmount = 0;
                let totalCount = 0;
                
                document.querySelectorAll('tbody tr').forEach(row => {
                    // 计算金额
                    const amountInput = row.querySelector('.amount-input');
                    let amountValue = amountInput.textContent.replace(/[^\d.-]/g, '');
                    amountValue = parseFloat(amountValue) || 0;
                    amountInput.textContent = amountValue.toFixed(2);
                    totalAmount += amountValue;
                    
                    // 计算票据张数
                    const countInput = row.querySelector('.item-count');
                    let countValue = countInput.textContent.replace(/[^\d]/g, '');
                    countValue = parseInt(countValue) || 0;
                    countInput.textContent = countValue;
                    totalCount += countValue;
                });
                
                document.getElementById('totalAmount').textContent = totalAmount.toFixed(2);
                document.getElementById('totalCount').textContent = totalCount;
                document.getElementById('receiptCount').textContent = totalCount;
            }
            
            // 添加新行
            document.getElementById('addRowBtn').addEventListener('click', function() {
                const tbody = document.getElementById('itemsTable');
                const rows = tbody.querySelectorAll('tr');
                const newRow = rows[rows.length - 1].cloneNode(true);
                
                // 更新序号
                const newRowNumber = rows.length + 1;
                newRow.querySelector('td:first-child').textContent = newRowNumber;
                
                // 清空内容
                newRow.querySelector('.item-content').textContent = '';
                newRow.querySelector('.amount-input').textContent = '0.00';
                newRow.querySelector('.item-count').textContent = '1';
                newRow.querySelector('.item-note').textContent = '';
                
                // 添加删除按钮事件
                const deleteBtn = newRow.querySelector('.delete-row-btn');
                deleteBtn.addEventListener('click', function() {
                    tbody.removeChild(newRow);
                    updateRowNumbers();
                    updateTotals();
                });
                
                tbody.appendChild(newRow);
                
                // 添加事件监听
                newRow.querySelector('.amount-input').addEventListener('input', updateTotals);
                newRow.querySelector('.item-count').addEventListener('input', updateTotals);
            });
            
            // 更新行号
            function updateRowNumbers() {
                const rows = document.querySelectorAll('#itemsTable tr');
                rows.forEach((row, index) => {
                    row.querySelector('td:first-child').textContent = index + 1;
                });
            }
            
            // 初始删除按钮事件
            document.querySelector('.delete-row-btn').addEventListener('click', function() {
                const tbody = document.getElementById('itemsTable');
                const rows = tbody.querySelectorAll('tr');
                if (rows.length > 1) {
                    tbody.removeChild(rows[rows.length - 1]);
                    updateTotals();
                }
            });
            
            // 日期输入限制
            document.querySelectorAll('.date-input').forEach(input => {
                input.addEventListener('input', function() {
                    this.value = this.value.replace(/[^0-9]/g, '');
                    
                    // 年、月、日长度限制
                    if (this.id === 'yearInput' && this.value.length > 4) {
                        this.value = this.value.slice(0, 4);
                    } else if ((this.id === 'monthInput' || this.id === 'dayInput') && this.value.length > 2) {
                        this.value = this.value.slice(0, 2);
                    }
                    
                    // 月份范围限制
                    if (this.id === 'monthInput' && parseInt(this.value) > 12) {
                        this.value = '12';
                    }
                    
                    // 日期范围限制
                    if (this.id === 'dayInput' && parseInt(this.value) > 31) {
                        this.value = '31';
                    }
                });
            });
            
            // 背景选择功能
            const bgOptions = document.querySelectorAll('.bg-option');
            const pasteSheet = document.getElementById('pasteSheet');
            
            bgOptions.forEach(option => {
                option.addEventListener('click', function() {
                    // 移除所有active类
                    bgOptions.forEach(opt => opt.classList.remove('active'));
                    // 添加当前active类
                    this.classList.add('active');
                    // 更新粘贴单背景
                    const bgClass = this.getAttribute('data-bg');
                    // 移除所有背景类
                    pasteSheet.className = 'paste-sheet';
                    // 添加选中的背景类
                    pasteSheet.classList.add(bgClass);
                });
            });
            
            // 初始化总计
            updateTotals();
        });
    </script>
</body>
</html>